package com.flower8.controller;

import com.flower8.dto.SysLogsDto;
import com.flower8.service.SysLogService;
import com.flower8.utils.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.flower8.controller.BaseController.logAfter;
import static com.flower8.controller.BaseController.logBefore;

/**
 * 日志控制层
 * @Author: tanglin
 * @Date: 2019/09/02 16:15
 * @Version: 1.0
 */
@RestController
@RequestMapping("/logs")
public class SysLogsController {
	private static final Logger log = LoggerFactory.getLogger(SysLogsController.class);

	@Autowired
	private SysLogService sysLogService;

	/**
	 * 查询所有日志信息
	 * @param request
	 * @return
	 */
	@RequestMapping("/getList")
	public Object list(HttpServletRequest request) {
		logBefore(log, "++++++++++ 查询所有日志信息列表 ++++++++++");
		PageData pd = new PageData(request);

		pd.put("start", Integer.parseInt( pd.get("start").toString() ) );
		pd.put("length", Integer.parseInt( pd.get("length").toString() ) );

		Map res = new HashMap();
		try {
			List<SysLogsDto> logList =  sysLogService.getLogList(pd);
			int total = sysLogService.getLogListCount(pd);
			res.put("data", logList);
			res.put("recordsTotal", total);   //dataTable分页需要
			res.put("recordsFiltered", total);//dataTable分页需要
			res.put("draw", pd.get("draw").toString());//dataTable分页需要
			logAfter(log);
		}catch (Exception e){
			e.printStackTrace();
			log.error(e.getMessage());
		}
		return res;
	}

}
